#!/bin/bash
set -e

AS_JSON=/usr/share/clearwater/ellis/web-content/js/app-servers.json
TMP_AS_JSON=/tmp/app-servers.json.$$

id=$(cut -d/ -f2 <<< $JUJU_REMOTE_UNIT)

# Delete the entry for this relation peer.
rm -f $AS_JSON.$id

{
  # Open the JSON body
  echo {
  # Concatenate all the AS files,
  # remove any duplicates (on the first field) and then
  # remove a comma from the end of the last line.
  cat $AS_JSON.* 2>/dev/null |
  sort -k 1,1 -u |
  sed -e '$ s/,$//'
  # Close the JSON body
  echo }
} > $TMP_AS_JSON
mv $TMP_AS_JSON $AS_JSON
